authApp.value('page', 'material_apply_auth'); 
authApp.controller("fillApplyController",function($scope,$http,$location,$getQueryString,Upload, $timeout,$sce,$cookieStore,$cookies){
	
	$scope.isUpload=true;
	
	$scope.zpbg=$sce.trustAsHtml("上传自评报告电子版");
	$scope.jyfb=$sce.trustAsHtml("上传电子版");
	$scope.zzhb=$sce.trustAsHtml("上传电子版");
	$scope.zyzz=$sce.trustAsHtml("上传电子版");
	$scope.Fid=$getQueryString("pid");	
	$scope.state=$getQueryString("state");	
	$scope.schoolId=$getQueryString("schoolId");
	if($scope.state=="1"){
		$scope.isUpload=false;
	}	
	$scope.zpbgPath="";
	$scope.jyfbPath="";
	$scope.zzhbPath="";
	$scope.zyzzPath="";
		
	$http({
		method:'get',
		url:apis.queryApplyFileBySchoolId1,
		params:{'schoolid':$scope.schoolId},
	}).success(function(response){
		$scope.zpbg=$sce.trustAsHtml(response.data.selfEvaluationFilename);
		$scope.jyfb=$sce.trustAsHtml(response.data.employmentDistributionFilename);
		$scope.zzhb=$sce.trustAsHtml(response.data.specialtyConstructionFilename);
		$scope.zyzz=$sce.trustAsHtml(response.data.supportingMaterialsFilename);
		$scope.zpbgPath=response.data.selfEvaluationFilepath;
		$scope.jyfbPath=response.data.employmentDistributionFilepath;
		$scope.zzhbPath=response.data.specialtyConstructionFilepath;
		$scope.zyzzPath=response.data.supportingMaterialsFilepath;
		$scope.zpbgN=response.data.selfEvaluationFilename;
		$scope.jyfbN=response.data.employmentDistributionFilename;
		$scope.zzhbN=response.data.specialtyConstructionFilename;
		$scope.zyzzN=response.data.supportingMaterialsFilename;
	})
	
/*	$scope.getJudgeList=function(){
		$http({
			method:'post',
			url:apis.queryAllTwice,
			data:{'schoolId':$cookies.get("schoolPid"),'loginType':$cookies.get("majorType"),'major':$cookies.get("major")},
		}).success(function(response){
			$scope.judeAllList=response.data;
		})
	}
	$scope.getJudgeList();*/
	
	$scope.getJudgeList=function(){
		$http({
			method:'get',
			url:apis.queryItemInfoByFid1,
			params:{'pid1':$getQueryString("pid"),'schoolId':$scope.schoolId},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.judeAllList=response.data.list;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	
	$scope.getJudgeList();
	
	$scope.turnToApply=function(pid){
		location.href="basic_applyauth.html?pid="+pid+"&schoolId="+$scope.schoolId+"&Fid="+$scope.Fid+"&state="+$scope.state;
	}
	
	/*$scope.turnToRead=function(pid){
		location.href="applyauth.html?pid="+pid+"&action=read";
	}*/
	
	$scope.saveAll=function(){
		if($scope.zpbgPath!=""&&$scope.jyfbPath!=""&&$scope.zzhbPath!=""&&$scope.zyzzPath!=""){
			var reqData={
					'schoolId':$cookies.get("schoolPid"),
					'selfEvaluationFilepath':$scope.zpbgPath,
					'selfEvaluationFilename':$scope.zpbgN,
					'employmentDistributionFilepath':$scope.jyfbPath,
					'employmentDistributionFilename':$scope.jyfbN,
					'specialtyConstructionFilepath':$scope.zzhbPath,
					'specialtyConstructionFilename':$scope.zzhbN,
					'supportingMaterialsFilepath':$scope.zyzzPath,
					'supportingMaterialsFilename':$scope.zyzzN,
					'loginType':$cookies.get("majorType")
				};
			$http({
				method:'post',
				url:apis.insertApplyFile,
				data:reqData,
			}).success(function(response){
				alert("保存成功！");
			})
		}else{
			alert("请先上传所有的文档");
		}
	}
	
	$scope.uploadFiles= function(file, errFiles,index) {
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];
        if (file) {
            file.upload = Upload.upload({
                url: apis.uploadTask,
                data: {file: file}
            });

            file.upload.then(function (response) {
                $timeout(function () {
                	if(response.data.code=="000000"){
                		if(index=="0"){
                			$scope.zpbgN=file.name;
                			$scope.zpbg=$sce.trustAsHtml(file.name);
                			$scope.zpbgPath=response.data.data;
                		}
                		if(index=="1"){
                			$scope.jyfbN=file.name;
                			$scope.jyfb=$sce.trustAsHtml(file.name);
                			$scope.jyfbPath=response.data.data;
                		}
                		if(index=="2"){
                			$scope.zzhbN=file.name;
                			$scope.zzhb=$sce.trustAsHtml(file.name);
                			$scope.zzhbPath=response.data.data;
                		}
                		if(index=="3"){
                			$scope.zyzzN=file.name;
                			$scope.zyzz=$sce.trustAsHtml(file.name);
                			$scope.zyzzPath=response.data.data;
                		}
                		file.result = response.data;	
                	}
                	else
                		alert(response.data.errorMsg);
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * 
                evt.loaded / evt.total));
            });
        }
    }
})